﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_Dispatch : System.Web.UI.Page
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        ZSql zs = new ZSql();
        if (!IsPostBack)
        {
            lblDh.Text = initBarCode();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            initDept();
            txtdate.Attributes.Add("onclick", "SelectDate('txtDate')");
            txtDate1.Attributes.Add("onclick", "SelectDate('txtDate1')");
            yydate.Attributes.Add("onclick", "SelectDate('yydate')");
            btnAdd.Attributes.Add("OnClick", "return checkadd(this.form)");
            if (Request["action"] == "add")
            {
               
              
              
            }
            if (Request["action"] == "addedit")
            {
                initData();
            }
            if (Request["action"] == "yjsg")
            {
               
                initData();
            }
            if (Request["action"] == "fwz")
            {
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                initUser();
                initData();
                btnAdd.Visible = false;
            }
            if (Request["action"] == "wc")
            {
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                initUser();
                initData();
                btnAdd.Visible = false;
                btnallprint.Visible = false;
            }
        }
    }
    private void initUser()
    {
        ZSql z = new ZSql();
        z.Open("select * from CS_Mailadd where Maintaddid=" + Request["id"]).ToString();


            ddlDept.SelectedValue = z.m_table.Rows[0]["deptid"].ToString();

            ZSql name = new ZSql();
            name.Open("select StaffId,StaffName from CS_Staff where deptid=" + z.m_table.Rows[0]["deptid"].ToString());
            ddlName.DataSource = name.m_table;
            ddlName.DataTextField = "StaffName";
            ddlName.DataValueField = "StaffId";
            ddlName.DataBind();
            ddlName.Items.Insert(0, new ListItem("—选派员工—", "0"));
            name.Close();

            ddlName.SelectedValue = z.m_table.Rows[0]["userid"].ToString();
        z.Close();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ZSql zs = new ZSql();
        ZSql addupdate = new ZSql();
        ZSql pg = new ZSql();
        btnAdd.Attributes.Add("OnClick", "return checkadd(this.form)");

            string id = zs.GetScalar("insert into CS_Mailadd(MaintCode,Guestname,Guestmen,Guesttel,Guestaddress,MaintDate,addUserName,dispTime,cominame,comidband,comidSpe,maitmen,maintdelt,userid,deptid,isadd,ismail,ispg,yydate,sjfj,xlh1,xlh2) values('" + lblDh.Text + "','" + Common.FormatParameter(txtGuestName.Text) + "','" + Common.FormatParameter(txtManName.Text) + "','" + Common.FormatParameter(txtPhone.Text) + "','" + Common.FormatParameter(txtAddress.Text) + "','" + Common.FormatParameter(txtdate.Text) + "','" + Common.FormatParameter(lblName.Text) + "','" + Common.FormatParameter(txtDate1.Text) + "','" + Common.FormatParameter(txtComoName.Text) + "','" + Common.FormatParameter(txtBrand.Text) + "','" + Common.FormatParameter(txtSpeci.Text) + "','" + ddlName.SelectedItem.Text + "','" + Common.FormatParameter(txtbj.Text) + "'," + ddlName.SelectedValue + "," + ddlDept.SelectedValue + ",1,1,1,'" + Common.FormatParameter(yydate.Text) + "','" + Common.FormatParameter(txtsjfj.Text) + "','" + Common.FormatParameter(txtxlh1.Text) + "','" + Common.FormatParameter(txtxlh2.Text) + "');select nid = @@identity").ToString();
            zs.Close();
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + id + "','" + Request["moduid"] + "')", true);
            //Response.Redirect("ServiceGuestAdd.aspx?moduid=" + Request["moduid"]);
        if (Request["action"] == "addedit")
        {
            addupdate.Open("update CS_Mailadd set Guestname='" + Common.FormatParameter(txtGuestName.Text) + "',Guestmen='" + Common.FormatParameter(txtManName.Text) + "',Guesttel='" + Common.FormatParameter(txtPhone.Text) + "',Guestaddress='" + Common.FormatParameter(txtAddress.Text) + "',MaintDate='" + Common.FormatParameter(txtdate.Text) + "',addEditUserName='" + Common.FormatParameter(lblName.Text) + "',dispTime='" + Common.FormatParameter(txtDate1.Text) + "',cominame='" + Common.FormatParameter(txtComoName.Text) + "',comidband='" + Common.FormatParameter(txtBrand.Text) + "',comidSpe='" + Common.FormatParameter(txtSpeci.Text) + "',maintdelt='" + Common.FormatParameter(txtbj.Text) + "',maitmen='" + ddlName.SelectedItem.Text + "',userid=" + ddlName.SelectedValue + ",deptid=" + ddlDept.SelectedValue + ",yydate='" + Common.FormatParameter(yydate.Text) + "',sjfj='" + Common.FormatParameter(txtsjfj.Text) + "',xlh1='" + Common.FormatParameter(txtxlh1.Text) + "',xlh2='" + Common.FormatParameter(txtxlh2.Text) + "' where Maintaddid=" + Request["id"] + "");
            addupdate.Close();
            Response.Redirect("ServiceGuestAdd.aspx?moduid=" + Request["moduid"]);
        }
        if (Request["action"] == "yjsg")
        {
            pg.Open("update CS_Mailadd set ispg=1,kddate='" + DateTime.Now.ToShortDateString() + "',kdmen='" + Server.UrlDecode(Request.Cookies["user"].Values["realname"]) + "',maitmen='"+ddlName.SelectedItem.Text.ToString()+"',userid="+ddlName.SelectedValue+",deptid="+ddlDept.SelectedValue+" where  Maintaddid=" + Request["id"] + "");
        }

        zs.Close();
        //  Response.Redirect("MaintDispatch.aspx?moduid=" + Request["moduid"]);
    }

    private void initDate()
    {
        txtdate.Text = DateTime.Now.ToShortDateString();
    }
    private void initDept()
    {


        ZSql dept = new ZSql();
        dept.Open("select deptid,deptname from Department");

        ddlDept.DataSource = dept.m_table;
        ddlDept.DataTextField = "deptname";
        ddlDept.DataValueField = "deptid";
        ddlDept.DataBind();

        ddlDept.Items.Insert(0, new ListItem("—选择部门—", "0"));
        ddlName.Items.Insert(0, new ListItem("—选派员工—", "0"));
        dept.Close();
    }
    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        

        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=" + ddlDept.SelectedValue);
        ddlName.DataSource = name.m_table;
        ddlName.DataTextField = "StaffName";
        ddlName.DataValueField = "StaffId";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("—选派员工—", "0"));
        name.Close();
    }

    private string initBarCode()
    {
        ZSql zs = new ZSql();
        string id = "wx" + DateTime.Now.ToString("yyyyMMdd") + "-";
        zs.Open("select max(Maintaddid) as a from CS_Mailadd");
        if (string.IsNullOrEmpty(zs.m_table.Rows[0]["a"].ToString()))
        {
            id += 1;
        }
        else
        {
            id += Convert.ToInt32(zs.m_table.Rows[0]["a"]) + 1;
        }

        return id;

    }

    private void initData()
    {
        
        ZSql zs = new ZSql();
        ZSql edit = new ZSql();
        ZSql fwz = new ZSql();
        if (Request["action"] == "addedit")
        {
            initUser();
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            zs.Open("select *,Convert(char(10),dispTime,120) as Date,Convert(char(10),MaintDate,120) as Date1,Convert(char(10),yydate,120) as Date2 from CS_Mailadd where isadd=1 and Maintaddid=" + Request["id"] + " order by Maintaddid desc");

        txtGuestName.Text = zs.m_table.Rows[0]["Guestname"].ToString();
        txtManName.Text = zs.m_table.Rows[0]["Guestmen"].ToString();
        txtPhone.Text = zs.m_table.Rows[0]["Guesttel"].ToString();
        txtAddress.Text = zs.m_table.Rows[0]["Guestaddress"].ToString();
        txtBrand.Text = zs.m_table.Rows[0]["comidband"].ToString();
        txtComoName.Text = zs.m_table.Rows[0]["cominame"].ToString();
        txtSpeci.Text = zs.m_table.Rows[0]["comidSpe"].ToString();
        txtDate1.Text = zs.m_table.Rows[0]["Date"].ToString();
        txtdate.Text = zs.m_table.Rows[0]["Date1"].ToString();
        yydate.Text = zs.m_table.Rows[0]["Date2"].ToString();
        //lblState.Text = zs.m_table.Rows[0]["devicstate"].ToString();
        //txtflag.Text = zs.m_table.Rows[0]["AuditState"].ToString();
        txtbj.Text = zs.m_table.Rows[0]["maintdelt"].ToString();
        txtsjfj.Text = zs.m_table.Rows[0]["sjfj"].ToString();
        txtxlh1.Text = zs.m_table.Rows[0]["xlh1"].ToString();
        txtxlh2.Text = zs.m_table.Rows[0]["xlh2"].ToString();
        zs.Close();
          }
          if (Request["action"] == "yjsg")
          {
              edit.Open("select *,Convert(char(10),dispTime,120) as Date,Convert(char(10),MaintDate,120) as Date1,Convert(char(10),yydate,120) as Date2 from CS_Mailadd where ismail=1 and Maintaddid=" + Request["id"] + " order by Maintaddid desc");
              txtGuestName.Text = edit.m_table.Rows[0]["Guestname"].ToString();
              txtManName.Text = edit.m_table.Rows[0]["Guestmen"].ToString();
              txtPhone.Text = edit.m_table.Rows[0]["Guesttel"].ToString();
              txtAddress.Text = edit.m_table.Rows[0]["Guestaddress"].ToString();
              txtBrand.Text = edit.m_table.Rows[0]["comidband"].ToString();
              txtComoName.Text = edit.m_table.Rows[0]["cominame"].ToString();
              txtSpeci.Text = edit.m_table.Rows[0]["comidSpe"].ToString();
              txtDate1.Text = edit.m_table.Rows[0]["Date"].ToString();
              txtdate.Text = edit.m_table.Rows[0]["Date1"].ToString();
              yydate.Text = edit.m_table.Rows[0]["Date2"].ToString();
              //lblState.Text = zs.m_table.Rows[0]["devicstate"].ToString();
              //txtflag.Text = zs.m_table.Rows[0]["AuditState"].ToString();
              txtbj.Text = edit.m_table.Rows[0]["maintdelt"].ToString();
              txtsjfj.Text = edit.m_table.Rows[0]["sjfj"].ToString();
              txtxlh1.Text = edit.m_table.Rows[0]["xlh1"].ToString();
              txtxlh2.Text = edit.m_table.Rows[0]["xlh2"].ToString();

              if (edit.m_table.Rows[0]["Date1"].ToString() == "")
              {
                  txtdate.Text = DateTime.Now.ToString();
                 
              }
              else
              {

                  txtdate.Text = edit.m_table.Rows[0]["Date1"].ToString();
              }
              if (edit.m_table.Rows[0]["Date2"].ToString() == "")
              {
                  yydate.Text = DateTime.Now.ToString();
              }
              else
              {
                  yydate.Text = edit.m_table.Rows[0]["Date2"].ToString();
              }

              edit.Close();
          }
          if (Request["action"] == "fwz" || Request["action"] == "wc")
          {
              fwz.Open("select *,Convert(char(10),dispTime,120) as Date,Convert(char(10),MaintDate,120) as Date1,Convert(char(10),yydate,120) as Date2 from CS_Mailadd where ispg=1 and Maintaddid=" + Request["id"] + " order by Maintaddid desc");
              txtGuestName.Text = fwz.m_table.Rows[0]["Guestname"].ToString();
              txtManName.Text = fwz.m_table.Rows[0]["Guestmen"].ToString();
              txtPhone.Text = fwz.m_table.Rows[0]["Guesttel"].ToString();
              txtAddress.Text = fwz.m_table.Rows[0]["Guestaddress"].ToString();
              txtBrand.Text = fwz.m_table.Rows[0]["comidband"].ToString();
              txtComoName.Text = fwz.m_table.Rows[0]["cominame"].ToString();
              txtSpeci.Text = fwz.m_table.Rows[0]["comidSpe"].ToString();
              txtDate1.Text = fwz.m_table.Rows[0]["Date"].ToString();
              txtdate.Text = fwz.m_table.Rows[0]["Date1"].ToString();
              yydate.Text = fwz.m_table.Rows[0]["Date2"].ToString();
              //lblState.Text = zs.m_table.Rows[0]["devicstate"].ToString();
              //txtflag.Text = zs.m_table.Rows[0]["AuditState"].ToString();
              txtbj.Text = fwz.m_table.Rows[0]["maintdelt"].ToString();
              txtsjfj.Text = fwz.m_table.Rows[0]["sjfj"].ToString();
              txtxlh1.Text = fwz.m_table.Rows[0]["xlh1"].ToString();
              txtxlh2.Text = fwz.m_table.Rows[0]["xlh2"].ToString();
              ddrylyy.SelectedItem.Text = fwz.m_table.Rows[0]["ylyy"].ToString();
              sertype.SelectedItem.Text = fwz.m_table.Rows[0]["fwlx"].ToString();
              ddlserfs.SelectedItem.Text = fwz.m_table.Rows[0]["jxfs"].ToString();
              ddlbxqk.SelectedItem.Text = fwz.m_table.Rows[0]["bxfs"].ToString();
              txtwxfy.Text = fwz.m_table.Rows[0]["mailfy"].ToString().Substring(0, fwz.m_table.Rows[0]["mailfy"].ToString().Length-2);
              txtbjfy.Text = fwz.m_table.Rows[0]["bjfy"].ToString().Substring(0, fwz.m_table.Rows[0]["bjfy"].ToString().Length - 2);
              txtclfy.Text = fwz.m_table.Rows[0]["clfy"].ToString().Substring(0, fwz.m_table.Rows[0]["clfy"].ToString().Length - 2);
              txtqtfy.Text = fwz.m_table.Rows[0]["qtfy"].ToString().Substring(0, fwz.m_table.Rows[0]["qtfy"].ToString().Length - 2);
              txtqyfysm.Text = fwz.m_table.Rows[0]["qtfysm"].ToString();
              txtzr.Text = fwz.m_table.Rows[0]["fyall"].ToString().Substring(0, fwz.m_table.Rows[0]["fyall"].ToString().Length - 2);
              txtbjms.Text = fwz.m_table.Rows[0]["bjms"].ToString();
              txtbjzyh.Text = fwz.m_table.Rows[0]["bjzyh"].ToString();
              ddlComt.SelectedItem.Text = fwz.m_table.Rows[0]["GuestCom"].ToString();
              txtyj.Text = fwz.m_table.Rows[0]["Guestconvent"].ToString();
              txtylyy.Text = fwz.m_table.Rows[0]["ylyybz"].ToString();
              txtGuestYJ.Text = fwz.m_table.Rows[0]["ylyybz"].ToString();
              lblName.Text = fwz.m_table.Rows[0]["jsman"].ToString();
              if (fwz.m_table.Rows[0]["fwcs"].ToString() == "")
              {
                  txtdate.Text = DateTime.Now.ToString();

              }
              else
              {

                  txtdate.Text = fwz.m_table.Rows[0]["Date1"].ToString();
              }
              if (fwz.m_table.Rows[0]["Date2"].ToString() == "")
              {
                  yydate.Text = DateTime.Now.ToString();
              }
              else
              {
                  yydate.Text = fwz.m_table.Rows[0]["Date2"].ToString();
              }
              fwz.Close();
          }
    }
    //protected void btndy_Click(object sender, EventArgs e)
    //{
    //    if (Request["action"] == "yjsg")
    //    {
    //        ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + Request["id"] + "')", true);
    //    }
    //    if (Request["action"] == "add" )
    //    {
    //        //ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + lblDh.Text + "','" + Common.FormatParameter(txtCode.Text) + "','" + Common.FormatParameter(txtGuestName.Text) + "','" + Common.FormatParameter(txtManName.Text) + "','" + Common.FormatParameter(txtPhone.Text) + "','" + Common.FormatParameter(txtAddress.Text) + "','" + Common.FormatParameter(txtdate.Text) + "','" + Common.FormatParameter(lblName.Text) + "','" + Common.FormatParameter(txtDate1.Text) + "','" + Common.FormatParameter(txtUnits.Text) + "','" + Common.FormatParameter(txtComoName.Text) + "','" + Common.FormatParameter(txtBrand.Text) + "','" + Common.FormatParameter(txtSpeci.Text) + "','" + ddlName.SelectedItem.Text + "','" + Common.FormatParameter(txtbj.Text) + "','" + Common.FormatParameter(yydate.Text) + "','" + Common.FormatParameter(txtsjfj.Text) + "','" + Common.FormatParameter(txtxlh1.Text) + "','" + Common.FormatParameter(txtxlh2.Text) + "')", true);
    //        ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + Request["id"] + "')", true);
    //    }
    //}
    //protected void likadd_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("ServiceGuestAdd.aspx?moduid=" + Request["moduid"] + "");
    //}
    //protected void likypd_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("ServiceGuestSale.aspx?moduid=" + Request["moduid"] + "");
    //}
    //protected void likyjsd_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("ServiceGuestreamail.aspx?moduid=" + Request["moduid"] + "");
    //}
    //protected void likfwz_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("ServiceGuestfwz.aspx?moduid=" + Request["moduid"] + "");
    //}
    protected void btnall_Click(object sender, EventArgs e)
    {
        string servertype = "";
        string serfs = "";
        string ylyy="";
        string bxqk = "";
        string khpf = "";
        if (sertype.SelectedValue == "0")
        {
            servertype = "空";
        }
        else
        {
            servertype = sertype.SelectedItem.Text;
        }
        if (ddlserfs.SelectedValue == "0")
        {
            serfs = "空";
        }
        else
        {
            serfs = ddlserfs.SelectedItem.Text;
        }
        if (ddrylyy.SelectedValue == "0")
        {
            ylyy = "空";
        }
        else
        {
            ylyy = ddrylyy.SelectedItem.Text;
        }
        if (ddlbxqk.SelectedValue == "0")
        {
            bxqk = "空";
        }
        else
        {
            bxqk = ddlbxqk.SelectedItem.Text;
        }
        if (ddlComt.SelectedValue == "0")
        {
            khpf = "空";
        }
        else
        {
            khpf = ddlComt.SelectedItem.Text;
        }
        ZSql fwzedit = new ZSql();
        if (Request["action"] == "fwz")
        {
            fwzedit.Open("update CS_Mailadd set Guestname='" + Common.FormatParameter(txtGuestName.Text) + "',Guestmen='" + Common.FormatParameter(txtManName.Text) + "',Guesttel='" + Common.FormatParameter(txtPhone.Text) + "',Guestaddress='" + Common.FormatParameter(txtAddress.Text) + "',MaintDate='" + Common.FormatParameter(txtdate.Text) + "',addEditUserName='" + Common.FormatParameter(lblName.Text) + "',dispTime='" + Common.FormatParameter(txtDate1.Text) + "',cominame='" + Common.FormatParameter(txtComoName.Text) + "',comidband='" + Common.FormatParameter(txtBrand.Text) + "',comidSpe='" + Common.FormatParameter(txtSpeci.Text) + "',maintdelt='" + Common.FormatParameter(txtbj.Text) + "',maitmen='" + ddlName.SelectedItem.Text + "',userid=" + ddlName.SelectedValue + ",deptid=" + ddlDept.SelectedValue + ",yydate='" + Common.FormatParameter(yydate.Text) + "',sjfj='" + Common.FormatParameter(txtsjfj.Text) + "',xlh1='" + Common.FormatParameter(txtxlh1.Text) + "',xlh2='" + Common.FormatParameter(txtxlh2.Text) + "',fwlx='" + servertype + "',jxfs='" + serfs + "',ylyy='" + ylyy + "',ylyybz='" + Common.FormatParameter(txtylyy.Text) + "',bxfs='" + bxqk + "',fwcs='" + Common.FormatParameter(txtGuestYJ.Text) + "',mailfy=" + Common.FormatParameter(txtwxfy.Text) + ",bjfy=" + Common.FormatParameter(txtbjfy.Text) + ",clfy=" + Common.FormatParameter(txtclfy.Text) + ",qtfy=" + Common.FormatParameter(txtqtfy.Text) + ",qtfysm='" + Common.FormatParameter(txtqyfysm.Text) + "',fyall=" + Common.FormatParameter(txtzr.Text) + ",bjms='" + Common.FormatParameter(txtbjms.Text) + "',bjzyh='" + Common.FormatParameter(txtbjzyh.Text) + "',GuestCom='" + khpf + "',mailtc=" + Common.FormatParameter(txttc.Text) + ",Guestconvent='" + Common.FormatParameter(txtyj.Text) + "',EditUserName='" + lblName.Text + "',EditDate='"+DateTime.Now.ToString()+"' where Maintaddid=" + Request["id"] + "");
            fwzedit.Close();
            Response.Redirect("ServiceGuestfwz.aspx?moduid=" + Request["moduid"]);
        }
    }
    protected void btnallprint_Click(object sender, EventArgs e)
    {
        
        ZSql fz = new ZSql();
        if (ddrylyy.SelectedItem.Text == "维修完成")
        {
            fz.Open("update CS_Mailadd set isWC=1,wcdate='" + DateTime.Now.ToString() + "',jsman='" + lblName.Text + "' where Maintaddid=" + Request["id"] + "");
            fz.Close();
            Response.Redirect("ServiceGuestfwz.aspx?moduid=" + Request["moduid"]);
        }
        else
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd();", true);
            ClientScript.RegisterStartupScript(typeof(Page), "bb", "alert('遗留原因中未选择完成维修！');", true);

        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("selectGuestwc.aspx?GuestName=" + Request["name"] + "&moduid=" + Request["moduid"]);
    }
}
